﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using iver.Entity;
using DBEntity;

namespace iver.Controllers
{
    public class UserLogController : ApiController
    {
        public class ReqUserLog : ReqList
        {
            public string userName { get; set; }
            public string ipAddress { get; set; }
            public string targetName { get; set; }
            public string result { get; set; }
            public string betweenTime { get; set; }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns>jsonstr</returns>
        [HttpPost]
        public ResResult UserLogList([FromBody]ReqUserLog req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            DateTime? beginTime = null;
            DateTime? endTime = null;
            if (req.betweenTime != null)
            {
                string[] temp = req.betweenTime.Split(',');
                DateTime bt = Convert.ToDateTime(temp[0]);
                DateTime et = Convert.ToDateTime(temp[1]);
                beginTime = new DateTime(bt.Year, bt.Month, bt.Day, 0, 0, 0);
                endTime = new DateTime(et.Year, et.Month, et.Day, 23, 59, 59);

            }
            iverDB db = new iverDB();
            var list = (from u in db.sysUserLog
                        where
                          (req.targetName == null ? 1 == 1 : u.targetName.Contains(req.targetName))
                           && (req.result == null ? 1 == 1 : u.result.Contains(req.result))
                           && (req.ipAddress == null ? 1 == 1 : u.ipAddress == req.ipAddress)
                           && (req.userName == null ? 1 == 1 : u.userName == req.userName)
                           && (req.betweenTime == null ? 1 == 1 : (u.createTime >= beginTime && u.createTime <= endTime))
                        orderby u.id descending
                        select u);
            var arr = list.Skip(req.limit * (req.page - 1)).Take(req.limit);
            int count = list.Count();
            return new ResResult
            {
                success = true,
                result = new
                {
                    count,
                    arr,
                }
            };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="req">id数组</param>
        /// <returns></returns>
        [HttpPost]
        public ResResult UserLogDel([FromBody]ReqID<List<int>> req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            iverDB db = new iverDB();
            var list = db.sysUserLog.Where(u => req.id.Contains(u.id));
            int delCount = list.Count();
            if (delCount == 0)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            db.sysUserLog.RemoveRange(list);
            db.SaveChanges();

            return new ResResult
            {
                success = true,
                rescode = ResCode.SuccessDel,
                result = new
                {
                    delCount,
                },
            };
        }
    }
}